using System;
using System.Collections.Generic;
using System.Text;

namespace GuavaData
{
    public class Command : Object
    {
        public Operator root;
        internal Transaction container;
        private Dictionary<string, string> table_aliases;
        private int tableCount;

        public Command()
        {
            table_aliases = new Dictionary<string, string>();
            tableCount = 0;
            container = null;
        }

        public Command(Operator o) : this()
        {
            root = o;
        }

        public Command CloneState(Operator o)
        {
            Command c = new Command(o);
            c.tableCount = this.tableCount;
            foreach (KeyValuePair<string, string> p in table_aliases)
                c.table_aliases[p.Key] = p.Value;
            return c;
        }

        public string nextTableAlias(string table)
        {
            // Create the new alias and increment
            string newAlias = "T_" + tableCount.ToString();
            tableCount++;

            // Add the alias to the table and return the alias
            table_aliases.Add(newAlias, table);
            return newAlias;
        }

        public string nextLoopAlias
        {
            get
            {
                string retval = "t" + tableCount.ToString();
                tableCount++;
                return retval;
            }
        }

        public void NotifyChange(params Command[] onew)
        {
            this.container.NotifyChange(this, onew);
        }

    }
}
